/**
 * Created by zhoulin
 */

var _layer = layer;
var _Vue = Vue;
//dev :开发环境，prod:生产环境，test:测试环境
var _ENV = "dev";
var _DOMAIN = window.location.host;
if("manage.cncarav.com" == _DOMAIN){
    _ENV = "prod";
}else if("test.cncarav.com" == _DOMAIN){
    _ENV = "test";
}else {
    _ENV = "dev";
}

/**
 * get
 * @param that vue对象
 * @param url host+api + 参数
 * @param permission 权限，无权限为空，非空有权限
 */
var Get = function (that,url,permission) {
    _Vue.http.headers.common[_TOKEN_HEADER_VALUE] = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token)
    var index = _layer.load();
    //设置请求权限
    if(!empty(permission))
        _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = permission;

    return that.$http.get(url).then(function (response) {
        // 响应成功回调
        console.log(response.data);
        if(response.data.success){
            //更新本地token
            sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token,response.data.token);
        }
        //清空heaer中权限值
        _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = "";
        _layer.close(index);
        return response.data;

    }, function (response) {
        // 响应错误回调
        console.log(response.body)
        //清空heaer中权限值
        _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = "";
        _layer.close(index);
        _layer.msg("<span style='color:red'>"+response.body.msg+"</span>", {time: 1500, icon:5});
        if(_LOGIN_TIMEOUT_CODE == response.body.code){
            sessionStorageUtils.del(storageKey.sessionStorageKey.token);
            sessionStorageUtils.del(storageKey.sessionStorageKey.permCodes);
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200)
        }
        return ;
    });
}

/**
 * post
 * @param that vue对象
 * @param url host+api
 * @param param 参数
 * @param isJson 是否转json字符串 true false
 * @param permission 权限，无权限为空，非空有权限
 */
var Post = function (that,url,param,isJson,permission) {
    _Vue.http.headers.common[_TOKEN_HEADER_VALUE] = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token)
    var index = _layer.load();
    if(isJson)
        param = JSON.stringify(param);

    //设置请求权限
    if(!empty(permission))
        _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = permission;

    return that.$http.post(
        url,
        param,
        {emulateJSON: true})
        .then(function (response) {
            // 响应成功回调
            console.log(response.data);
            if(response.data.success){
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token,response.data.token);
            }
            //清空heaer中权限值
            _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = "";
            _layer.close(index);
            return response.data;

        }, function (response) {
            // 响应错误回调
            console.log(response.body)
            //清空heaer中权限值
            _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = "";
            _layer.close(index);
            _layer.msg("<span style='color:red'>"+response.body.msg+"</span>", {time: 1500, icon:5});
            if(_LOGIN_TIMEOUT_CODE == response.body.code){
                sessionStorageUtils.del(storageKey.sessionStorageKey.token);
                sessionStorageUtils.del(storageKey.sessionStorageKey.permCodes);
                setTimeout(function () {
                    window.location.href = _LOGIN_PAGE;
                },1200)
            }
            return ;
        });
}

/**
 * put
 * @param that vue对象
 * @param url host+api
 * @param param 参数
 * @param isJson 是否转json字符串 true false
 * @param permission 权限，无权限为空，非空有权限
 */
var Put = function (that,url,param,isJson,permission) {
    _Vue.http.headers.common[_TOKEN_HEADER_VALUE] = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token)
    var index = _layer.load();
    if(isJson)
        param = JSON.stringify(param);

    //设置请求权限
    if(!empty(permission))
        _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = permission;

    return that.$http.put(
        url,
        param,
        {emulateJSON: true})
        .then(function (response) {
            // 响应成功回调
            console.log(response.data);
            if(response.data.success){
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token,response.data.token);
            }
            //清空heaer中权限值
            _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = "";
            _layer.close(index);
            return response.data;

        }, function (response) {
            // 响应错误回调
            console.log(response.body)
            //清空heaer中权限值
            _Vue.http.headers.common[_PERM_CODING_HEADER_VALUE] = "";
            _layer.close(index);
            _layer.msg("<span style='color:red'>"+response.body.msg+"</span>", {time: 1500, icon:5});
            if(_LOGIN_TIMEOUT_CODE == response.body.code){
                sessionStorageUtils.del(storageKey.sessionStorageKey.token);
                sessionStorageUtils.del(storageKey.sessionStorageKey.permCodes);
                setTimeout(function () {
                    window.location.href = _LOGIN_PAGE;
                },1200)
            }
            return ;
        });
}

